package binarytree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/19 11:00
 * https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
 * 给定一个 N 叉树，返回其节点值的层序遍历。
 */
public class NTreeLevelOrder {

    class Node {
        int val;
        List<Node> children;

        public Node() {
        }

        public Node(int val) {
            this.val = val;
        }

        public Node(int val, List<Node> children) {
            this.val = val;
            this.children = children;
        }
    }


    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        Queue<Node> levelNodes = new LinkedList<>();
        if (root != null) {
            levelNodes.offer(root);
        }
        while (!levelNodes.isEmpty()) {
            List<Integer> temp = new ArrayList<>();
            int size = levelNodes.size();
            for (int i = 0; i < size; i++) {
                Node poll = levelNodes.poll();
                temp.add(poll.val);
                levelNodes.addAll(poll.children);
            }
            result.add(temp);
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
